#include "pch.h"
#include "HString.h"

void InitString(HString *s) {
	s->ch = NULL;
	s->length = 0;
}


void StrAssign(HString *s, const char *str) {
	int len = strlen(str);
	if (s->ch != NULL)
		free(s->ch);
	s->ch = (char*)malloc(sizeof(char)*len);
	assert(s->ch != NULL);
	for (int i = 0; i < len; i++)
		s->ch[i] = str[i];
	s->length = len;
}

int StrLength(HString *s) {
	return s->length;
}

void StrCopy(HString *s, HString *t) {
	if (s->ch != NULL)
		free(s->ch);
	int len = StrLength(t);
	s->ch = (char*)malloc(sizeof(char)*len);
	assert(s->ch != NULL);
	for (int i = 0; i < len; i++)
		s->ch[i] = t->ch[i];
	s->length = t->length;
}

bool StrEmpty(HString *s) {
	return s->length == 0;
}

